package com.bw.eurekaclient.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.bw.common.dto.UserDTO;
import com.bw.common.vo.ResultVO;
import com.bw.common.vo.UserVO;
import com.bw.eurekaclient.entity.User;
import com.bw.eurekaclient.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2021-10-12
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    IUserService iUserService;


    @Value("${foo}")
    private String foo;

    @RequestMapping("/list/page")
    public ResultVO getUser(@RequestBody UserDTO userDTO){
        log.info("读取config server 的配置文件的变量foo的值为:{}",foo);
        PageHelper.startPage(userDTO.getPageNO(), userDTO.getPageSize());
        Wrapper<User> wrapper = new EntityWrapper<User>();
        List<User> users = iUserService.selectList(wrapper);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return ResultVO.success(pageInfo);
    }

    @RequestMapping("/save")
    public ResultVO save(@RequestBody UserDTO userDTO){

        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        iUserService.insert(user);
        return ResultVO.success();
    }

    /**
     * 根据用户id 查询用户信息
     * @param userDTO
     * @return
     */
    @RequestMapping("/getUserID")
    public UserVO getUserID(@RequestBody UserDTO userDTO){
        User user = iUserService.selectById(userDTO.getId());
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return userVO;
    }

}
